package edu.paper.job.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.paper.job.domain.AjaxResult;
import edu.paper.job.domain.Company;
import edu.paper.job.domain.Employee;
import edu.paper.job.domain.Job;
import edu.paper.job.service.CompanyService;
import edu.paper.job.service.EmployeeService;
import edu.paper.job.service.JobService;

/**
 * 控制企业实体与视图之间的联系
 * @author bowen
 *
 */
@Controller
public class CompanyController {

	//企业的业务逻辑操作
	@Autowired
	private CompanyService companyService;
	
	//招聘信息简历
	@Autowired
	private JobService jobService;
	
	//雇佣关系业务逻辑
	@Autowired
	private EmployeeService employeeService;
	
	//spring bean工厂
	@Autowired
	private ApplicationContext applicationContext;
	
	/**
	 * 去企业注册页面
	 * @return
	 */
	@RequestMapping("/company/toEnroll.action")
	public String toEroll() {
		return "company/jCompanyRegister.jsp";
	}
	
	/**
	 * 企业注册
	 * @param company
	 * @return ajax结果	fail为注册失败，success为注册成功
	 */
	@RequestMapping("/company/enroll.action")
	public @ResponseBody AjaxResult enroll(Company company) {
		AjaxResult result = applicationContext.getBean(AjaxResult.class);
		if(companyService.register(company)) {
			//注册成功
			result.setResult(AjaxResult.SUCCESS);
		}else {
			//注册失败
			result.setResult(AjaxResult.FAIL);
		}
		return result;
	}
	
	/**
	 * 企业登录
	 * @param company
	 * @return ajax结果	fail为登录失败，success为登录成功
	 */
	@RequestMapping("/company/login.action")
	public @ResponseBody AjaxResult login(Company company) {
		AjaxResult result = applicationContext.getBean(AjaxResult.class);
		if(companyService.login(company) == null) {
			//登录失败
			result.setResult(AjaxResult.FAIL);
			return result;
		}else {
			//登陆成功
			result.setResult(AjaxResult.SUCCESS);
		}
		return result;
	}
	
	/**
	 * 去企业的主页面
	 * @param commpany
	 * @param model
	 * @return 主页面地址
	 */
	@RequestMapping("/company/home.action")
	public String home(Company company, Model model) {
		if(company.getUsername() == null) {
			company.setUsername("");
		}
		if(company.getPassword() == null) {
			company.setPassword("");
		}
		company = companyService.login(company);
		model.addAttribute("company", company);
		return "company/home.jsp";
	}
	
	/**
	 * 列出企业没删除的招聘信息
	 * @param companyId
	 * @param model
	 * @return	返回列表视图路径
	 */
	@RequestMapping("/company/list/job/{companyId}.action")
	public String list(@PathVariable Long companyId, Model model) {
		//搜索条件,企业Id，未被删除
		Job params = applicationContext.getBean(Job.class);
		params.setCompanyId(companyId);
		params.setIsDeleted(false);
		//查找招聘信息
		List<Job> dataList =  jobService.find(params);
		//把数据给页面
		model.addAttribute("dataList", dataList);
		model.addAttribute("companyId", companyId);
		return "company/jJobList.jsp";
		
	}
	
	/**
	 * 去添加招聘信息的界面
	 * @param companyId
	 * @param model
	 * @return 返回招聘信息视图的路径
	 */
	@RequestMapping("/company/toAdd/job/{companyId}.action")
	public String toAdd(@PathVariable Long companyId, Model model) {
		/*
		 * 将招聘信息的固定信息，传递给添加招聘信息的页面
		 */
		//获取去固定信息
		Company company = companyService.get(companyId);
		model.addAttribute("company", company);
		return "company/jJobAdd.jsp";
	}
	
	/**
	 * 保存招聘信息
	 * @param job
	 * @return 返回列表界面
	 */
	@RequestMapping("/company/save/job/{companyId}.action")
	public String save(@PathVariable Long companyId,Job job) {
		if(job.getIsPublic() == null) {//没选中时为空
			job.setIsPublic(false);
		}
		//保存招聘信息
		jobService.insertJob(job);
		return "redirect:/company/list/job/" + companyId + ".action";
	}
	
	/**
	 * 浏览招聘信息
	 * @param jobId
	 * @param model
	 * @return 返回浏览招聘信息的页面路径
	 */
	@RequestMapping("/company/view/job/{jobId}.action")
	public String view(@PathVariable Long jobId, Model model) {
		//找到要浏览的招聘信息
		Job job = jobService.get(jobId);
		model.addAttribute("job", job);
		return "company/jJobView.jsp";
	}
	
	/**
	 * 去修改招聘信息的界面
	 * @param jobId
	 * @param model
	 * @return 修改招聘信息的界面的路径
	 */
	@RequestMapping("/company/toUpdate/job/{jobId}.action")
	public String toUpdate(@PathVariable Long jobId, Model model) {
		//找到要修改的招聘信息
		Job job = jobService.get(jobId);
		model.addAttribute("job", job);
		return "company/jJobUpdate.jsp";
	}
	
	/**
	 * 更新招聘信息
	 * @param jobId
	 * @param job
	 * @return 返回招聘信息列表界面
	 */
	@RequestMapping("/company/update/job/{jobId}.action")
	public String update(@PathVariable Long jobId, Job job) {
		if(job.getIsPublic() == null) {//没选中时为空
			job.setIsPublic(false);
		}
		jobService.updateJob(job);
		return "redirect:/company/list/job/" + job.getCompanyId() + ".action";
	}
	
	/**
	 * 删除招聘信息
	 * @param jobId
	 * @return 返回招聘信息列表界面
	 */
	@RequestMapping("/company/delete/job/{jobId}.action")
	public String delete(@PathVariable Long jobId) {
		//找到要删除的招聘信息
		Job job = jobService.get(jobId);
		jobService.deleteJob(job);
		return "redirect:/company/list/job/" + job.getCompanyId() + ".action";
	}
	
	/**
	 * 去模糊查询招聘信息的页面
	 * @return 返回模糊查询招聘信息的页面的路径
	 */
	@RequestMapping("/company/toQuery/job/{applicantId}.action")
	public String toQuery(@PathVariable Long applicantId, Model model) {
		//找到企业发布的所有招聘信息
		Job job = applicationContext.getBean(Job.class);
		List<Job> dataList = jobService.queryJob(job);
		model.addAttribute("dataList", dataList);
		model.addAttribute("applicantId", applicantId);
		return "company/jJobQuery.jsp";
	}
	
	/**
	 * 模糊查询招聘信息
	 * @param job
	 * @param model
	 * @return 返回查询的数据和页面
	 */
	@RequestMapping("/company/query/job.action")
	public String query(Long applicantId, Job job, Model model) {
		//查询数据
		List<Job> dataList = jobService.queryJob(job);
		model.addAttribute("dataList", dataList);
		model.addAttribute("applicantId", applicantId);
		return "company/jJobQueryTable.jsp";
	}
	
	/**
	 * 列出企业的应聘信息
	 * @param companyId
	 * @param model
	 * @return 应聘的列表界面
	 */
	@RequestMapping("/company/list/employee/{companyId}.action")
	public String listEmployee(@PathVariable Long companyId, Model model) {
		//构建搜索条件
		Job job = applicationContext.getBean(Job.class);
		job.setCompanyId(companyId);
		Employee params = applicationContext.getBean(Employee.class);
		params.setJob(job);
		//用搜索条件获取数据
		List<Employee> dataList = employeeService.listCompanyEmployee(params);
		model.addAttribute("dataList", dataList);
		return "company/jEmployeeList.jsp";
	}
	
	/**
	 * 同意求职者
	 * @param companyId
	 * @param employeeId
	 * @return 返回应聘的列表界面
	 */
	@RequestMapping("/company/agree/employee/{companyId}/{employeeId}.action")
	public String agree(@PathVariable Long companyId, @PathVariable Long employeeId) {
		//准备更新条件
		Employee employee = applicationContext.getBean(Employee.class);
		employee.setId(employeeId);
		employeeService.agreeApplicant(employee);
		return "redirect:/company/list/employee/" + companyId + ".action";
	}

	/**
	 * 拒绝求职者
	 * @param companyId
	 * @param employeeId
	 * @return 返回应聘的列表界面
	 */
	@RequestMapping("/company/refuse/employee/{companyId}/{employeeId}.action")
	public String refuse(@PathVariable Long companyId, @PathVariable Long employeeId) {
		//准备更新条件
		Employee employee = applicationContext.getBean(Employee.class);
		employee.setId(employeeId);
		employeeService.refuseApplicant(employee);
		return "redirect:/company/list/employee/" + companyId + ".action";
	}
	
	/**
	 * 删除雇佣关系
	 * @param companyId
	 * @param employeeId
	 * @return 返回应聘的列表界面
	 */
	@RequestMapping("/company/delete/employee/{companyId}/{employeeId}.action")
	public String deleteEmployee(@PathVariable Long companyId, @PathVariable Long employeeId) {
		//准备删除条件
		Employee employee = applicationContext.getBean(Employee.class);
		employee.setId(employeeId);
		employeeService.deleteEmployeeByCompany(employee);
		return "redirect:/company/list/employee/" + companyId + ".action";
	}
	
}
